package com.cskaoyan.service;


import com.cskaoyan.bean.OrderExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.config.AliyunComponent;

import com.cskaoyan.constants.OrderStatusConstants;
import com.cskaoyan.mapper.OrderMapper;

import com.cskaoyan.mapper.UserMapper;


import com.cskaoyan.vo.ResVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WxServiceImpl implements WxService{
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    AliyunComponent aliyunComponent;

//
//    @Override
//    public ResVo queryUser(String token) {
//
//        Integer userId = JwtUtils.getUserIdByJwtToken(token);
//        //待收货的
//        OrderExample orderExampleUnrecv = new OrderExample();
//        orderExampleUnrecv.createCriteria()
//                .andDeletedEqualTo(false)
//                .andOrderStatusEqualTo(OrderStatusConstants.ORDER_SHIPPED)
//                .andUserIdEqualTo(userId);
//        int unrecv = (int) orderMapper.countByExample(orderExampleUnrecv);
//        //未支付
//        OrderExample orderExampleUnpay = new OrderExample();
//        orderExampleUnrecv.createCriteria().andPayTimeIsNull();
//        int  unpay = (int) orderMapper.countByExample(orderExampleUnpay);
//
//
//        //待评价
//        List<Short> unCommentStaus = new ArrayList<>();
//        unCommentStaus.add(OrderStatusConstants.USER_RECEIPT);
//        unCommentStaus.add(OrderStatusConstants.SYSTEM_RECEIPT);
//        OrderExample orderExampleUncomment = new OrderExample();
//        orderExampleUncomment.createCriteria()
//                .andDeletedEqualTo(false)
//                .andOrderStatusIn(unCommentStaus)
//                .andUserIdEqualTo(userId);
//
//        int uncomment = (int) orderMapper.countByExample(orderExampleUncomment);
//        //代发货
//        OrderExample orderExampleUnship = new OrderExample();
//        orderExampleUnship.createCriteria()
//                .andDeletedEqualTo(false)
//                .andOrderStatusEqualTo(OrderStatusConstants.ORDER_PAID)
//                .andUserIdEqualTo(userId);
//
//        int unship = (int) orderMapper.countByExample(orderExampleUnship);
//
//        ResVo resVo = new ResVo();
//        resVo.getOrder().setUnpaid( unpay);
//        resVo.getOrder().setUncomment(uncomment);
//        resVo.getOrder().setUnrecv(unrecv);
//        resVo.getOrder().setUnship(unship);
//
//        return resVo;
//
//    }

//    @Override
//    public LoginVo_wx  handlerUserRegister(UserRegisterVO userRegisterVO) {
//        //判断手机号是否已经被注册过
//        UserExample userExample = new UserExample();
//        userExample.createCriteria().andMobileEqualTo(userRegisterVO.getMobile());
//        int PhoneCount = (int) userMapper.countByExample(userExample);
//        if (PhoneCount > 0) {
//         // return new BaseRespVo.erro(501, MallConstant.USER_REGISTER_MOBILE_ERR);
//        }
//        //判断用户名是否已被使用
//        UserExample userExampleName = new UserExample();
//        userExampleName.createCriteria().andUsernameEqualTo(userRegisterVO.getUsername());
//        int nameCount = (int) userMapper.countByExample(userExampleName);
//        if (nameCount > 0) {
//            throw new GlobalException(501, MallConstant.USER_REGISTER_NAME_ERR);
//        }
//        //判断验证码是否正确
//        String code = stringRedisTemplate.opsForValue().get(userRegisterVO.getMobile());
//        if (!Objects.equals(code, userRegisterVO.getCode())) {
//            throw new GlobalException(501, MallConstant.USER_REGISTER_CODE_ERR);
//        }
//        User user = new User();
//        //加密密码
//        String enPassword = MD5.EnCode(userRegisterVO.getPassword());
//        user.setUsername(userRegisterVO.getUsername());
//        user.setPassword(enPassword);
//        user.setGender((byte) 0);
//        user.setUserLevel((byte) 0);
//        user.setNickname(userRegisterVO.getMobile());
//        user.setMobile(userRegisterVO.getMobile());
//        user.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80");
//        user.setStatus((byte) 0);
//        user.setAddTime(new Date());
//        user.setUpdateTime(new Date());
//        user.setDeleted(false);
//        user.setNickname(userRegisterVO.getUsername());
//        user.setLastLoginIp("1.1.1.1");
//        user.setBirthday(new Date());
//        int i = userMapper.insertSelective(user);
//        User users = userMapper.selectByPrimaryKey(user.getId());
//        LoginVo_wx loginVo_wx = new LoginVo_wx();
//        loginVo_wx.getUserInfo().setNickName(user.getNickname());
//        loginVo_wx.getUserInfo().setAvatarUrl(user.getAvatar());
//
//        String token = JwtUtils.getJwtToken(user.getUsername(), String.valueOf(user.getId()));
//
//        String tokenExpire = new Date().toString();
//
//        loginVo_wx.setToken(token);
//
//        loginVo_wx.setTokenExpire(tokenExpire);
//
//       return loginVo_wx;

 //   }


    @Override
    public ResVo queryUserDetails() {


        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer userId = user.getId();
        //待收货的
        OrderExample orderExampleUnrecv = new OrderExample();
        orderExampleUnrecv.createCriteria()
                .andDeletedEqualTo(false)
                .andOrderStatusEqualTo(OrderStatusConstants.ORDER_SHIPPED)
                .andUserIdEqualTo(userId);
        int unrecv = (int) orderMapper.countByExample(orderExampleUnrecv);
        //未支付
        OrderExample orderExampleUnpay = new OrderExample();
        orderExampleUnrecv.createCriteria().andPayTimeIsNull();
        int  unpay = (int) orderMapper.countByExample(orderExampleUnpay);


        //待评价
        List<Short> unCommentStaus = new ArrayList<>();
        unCommentStaus.add(OrderStatusConstants.USER_RECEIPT);
        unCommentStaus.add(OrderStatusConstants.SYSTEM_RECEIPT);
        OrderExample orderExampleUncomment = new OrderExample();
        orderExampleUncomment.createCriteria()
                .andDeletedEqualTo(false)
                .andOrderStatusIn(unCommentStaus)
                .andUserIdEqualTo(userId);

        int uncomment = (int) orderMapper.countByExample(orderExampleUncomment);
        //代发货
        OrderExample orderExampleUnship = new OrderExample();
        orderExampleUnship.createCriteria()
                .andDeletedEqualTo(false)
                .andOrderStatusEqualTo(OrderStatusConstants.ORDER_PAID)
                .andUserIdEqualTo(userId);

        int unship = (int) orderMapper.countByExample(orderExampleUnship);
        ResVo resVo = new ResVo();
        resVo.getOrder().setUncomment(uncomment);
        resVo.getOrder().setUnpaid(unpay);
        resVo.getOrder().setUnship(unship);
        resVo.getOrder().setUnrecv(unrecv);
        return resVo;

    }
}